home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / andere sprachen / perl5 / perl5.002 / pp_ctl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-27  |  49.2 KB  |  2,490 lines

  1. /*    pp_ctl.c
  2.  *
  3.  *    Copyright (c) 1991-1994, Larry Wall
  4.  *
  5.  *    You may distribute under the terms of either the GNU General Public
  6.  *    License or the Artistic License, as specified in the README file.
  7.  *
  8.  */
  9.  
  10. /*
  11.  * Now far ahead the Road has gone,
  12.  * And I must follow, if I can,
  13.  * Pursuing it with eager feet,
  14.  * Until it joins some larger way
  15.  * Where many paths and errands meet.
  16.  * And whither then?  I cannot say.
  17.  */
  18.  
  19. #include "EXTERN.h"
  20. #include "perl.h"
  21.  
  22. #ifndef WORD_ALIGN
  23. #define WORD_ALIGN sizeof(U16)
  24. #endif
  25.  
  26. static OP *doeval _((int gimme));
  27. static OP *dofindlabel _((OP *op, char *label, OP **opstack));
  28. static void doparseform _((SV *sv));
  29. static I32 dopoptoeval _((I32 startingblock));
  30. static I32 dopoptolabel _((char *label));
  31. static I32 dopoptoloop _((I32 startingblock));
  32. static I32 dopoptosub _((I32 startingblock));
  33. static void save_lines _((AV *array, SV *sv));
  34. static int sortcmp _((const void *, const void *));
  35. static int sortcv _((const void *, const void *));
  36.  
  37. static I32 sortcxix;
  38.  
  39. PP(pp_wantarray)
  40. {
  41.     dSP;
  42.     I32 cxix;
  43.     EXTEND(SP, 1);
  44.  
  45.     cxix = dopoptosub(cxstack_ix);
  46.     if (cxix < 0)
  47.     RETPUSHUNDEF;
  48.  
  49.     if (cxstack[cxix].blk_gimme == G_ARRAY)
  50.     RETPUSHYES;
  51.     else
  52.     RETPUSHNO;
  53. }
  54.  
  55. PP(pp_regcmaybe)
  56. {
  57.     return NORMAL;
  58. }
  59.  
  60. PP(pp_regcomp) {
  61.     dSP;
  62.     register PMOP *pm = (PMOP*)cLOGOP->op_other;
  63.     register char *t;
  64.     SV *tmpstr;
  65.     STRLEN len;
  66.  
  67.     tmpstr = POPs;
  68.     t = SvPV(tmpstr, len);
  69.  
  70.     /* JMR: Check against the last compiled regexp */
  71.     if ( ! pm->op_pmregexp  || ! pm->op_pmregexp->precomp
  72.     || strnNE(pm->op_pmregexp->precomp, t, len) 
  73.     || pm->op_pmregexp->precomp[len]) {
  74.     if (pm->op_pmregexp) {
  75.         pregfree(pm->op_pmregexp);
  76.         pm->op_pmregexp = Null(REGEXP*);    /* crucial if regcomp aborts */
  77.     }
  78.  
  79.     pm->op_pmflags = pm->op_pmpermflags;    /* reset case sensitivity */
  80.     pm->op_pmregexp = pregcomp(t, t + len, pm);
  81.     }
  82.  
  83.     if (!pm->op_pmregexp->prelen && curpm)
  84.     pm = curpm;
  85.     else if (strEQ("\\s+", pm->op_pmregexp->precomp))
  86.     pm->op_pmflags |= PMf_WHITE;
  87.  
  88.     if (pm->op_pmflags & PMf_KEEP) {
  89.     pm->op_pmflags &= ~PMf_RUNTIME;    /* no point compiling again */
  90.     hoistmust(pm);
  91.     cLOGOP->op_first->op_next = op->op_next;
  92.     }
  93.     RETURN;
  94. }
  95.  
  96. PP(pp_substcont)
  97. {
  98.     dSP;
  99.     register PMOP *pm = (PMOP*) cLOGOP->op_other;
  100.     register CONTEXT *cx = &cxstack[cxstack_ix];
  101.     register SV *dstr = cx->sb_dstr;
  102.     register char *s = cx->sb_s;
  103.     register char *m = cx->sb_m;
  104.     char *orig = cx->sb_orig;
  105.     register REGEXP *rx = cx->sb_rx;
  106.  
  107.     if (cx->sb_iters++) {
  108.     if (cx->sb_iters > cx->sb_maxiters)
  109.         DIE("Substitution loop");
  110.  
  111.     sv_catsv(dstr, POPs);
  112.     if (rx->subbase)
  113.         Safefree(rx->subbase);
  114.     rx->subbase = cx->sb_subbase;
  115.  
  116.     /* Are we done */
  117.     if (cx->sb_once || !pregexec(rx, s, cx->sb_strend, orig,
  118.                 s == m, Nullsv, cx->sb_safebase))
  119.     {
  120.         SV *targ = cx->sb_targ;
  121.         sv_catpvn(dstr, s, cx->sb_strend - s);
  122.  
  123.         (void)SvOOK_off(targ);
  124.         Safefree(SvPVX(targ));
  125.         SvPVX(targ) = SvPVX(dstr);
  126.         SvCUR_set(targ, SvCUR(dstr));
  127.         SvLEN_set(targ, SvLEN(dstr));
  128.         SvPVX(dstr) = 0;
  129.         sv_free(dstr);
  130.  
  131.         (void)SvPOK_only(targ);
  132.         SvSETMAGIC(targ);
  133.         PUSHs(sv_2mortal(newSViv((I32)cx->sb_iters - 1)));
  134.         LEAVE_SCOPE(cx->sb_oldsave);
  135.         POPSUBST(cx);
  136.         RETURNOP(pm->op_next);
  137.     }
  138.     }
  139.     if (rx->subbase && rx->subbase != orig) {
  140.     m = s;
  141.     s = orig;
  142.     cx->sb_orig = orig = rx->subbase;
  143.     s = orig + (m - s);
  144.     cx->sb_strend = s + (cx->sb_strend - m);
  145.     }
  146.     cx->sb_m = m = rx->startp[0];
  147.     sv_catpvn(dstr, s, m-s);
  148.     cx->sb_s = rx->endp[0];
  149.     cx->sb_subbase = rx->subbase;
  150.  
  151.     rx->subbase = Nullch;    /* so recursion works */
  152.     RETURNOP(pm->op_pmreplstart);
  153. }
  154.  
  155. PP(pp_formline)
  156. {
  157.     dSP; dMARK; dORIGMARK;
  158.     register SV *form = *++MARK;
  159.     register U16 *fpc;
  160.     register char *t;
  161.     register char *f;
  162.     register char *s;
  163.     register char *send;
  164.     register I32 arg;
  165.     register SV *sv;
  166.     char *item;
  167.     I32 itemsize;
  168.     I32 fieldsize;
  169.     I32 lines = 0;
  170.     bool chopspace = (strchr(chopset, ' ') != Nullch);
  171.     char *chophere;
  172.     char *linemark;
  173.     double value;
  174.     bool gotsome;
  175.     STRLEN len;
  176.  
  177.     if (!SvCOMPILED(form)) {
  178.     SvREADONLY_off(form);
  179.     doparseform(form);
  180.     }
  181.  
  182.     SvPV_force(formtarget, len);
  183.     t = SvGROW(formtarget, len + SvCUR(form) + 1);  /* XXX SvCUR bad */
  184.     t += len;
  185.     f = SvPV(form, len);
  186.     /* need to jump to the next word */
  187.     s = f + len + WORD_ALIGN - SvCUR(form) % WORD_ALIGN;
  188.  
  189.     fpc = (U16*)s;
  190.  
  191.     for (;;) {
  192.     DEBUG_f( {
  193.         char *name = "???";
  194.         arg = -1;
  195.         switch (*fpc) {
  196.         case FF_LITERAL:    arg = fpc[1]; name = "LITERAL";    break;
  197.         case FF_BLANK:    arg = fpc[1]; name = "BLANK";    break;
  198.         case FF_SKIP:    arg = fpc[1]; name = "SKIP";    break;
  199.         case FF_FETCH:    arg = fpc[1]; name = "FETCH";    break;
  200.         case FF_DECIMAL:    arg = fpc[1]; name = "DECIMAL";    break;
  201.  
  202.         case FF_CHECKNL:    name = "CHECKNL";    break;
  203.         case FF_CHECKCHOP:    name = "CHECKCHOP";    break;
  204.         case FF_SPACE:    name = "SPACE";        break;
  205.         case FF_HALFSPACE:    name = "HALFSPACE";    break;
  206.         case FF_ITEM:    name = "ITEM";        break;
  207.         case FF_CHOP:    name = "CHOP";        break;
  208.         case FF_LINEGLOB:    name = "LINEGLOB";    break;
  209.         case FF_NEWLINE:    name = "NEWLINE";    break;
  210.         case FF_MORE:    name = "MORE";        break;
  211.         case FF_LINEMARK:    name = "LINEMARK";    break;
  212.         case FF_END:    name = "END";        break;
  213.         }
  214.         if (arg >= 0)
  215.         fprintf(stderr, "%-16s%ld\n", name, (long) arg);
  216.         else
  217.         fprintf(stderr, "%-16s\n", name);
  218.     } )
  219.     switch (*fpc++) {
  220.     case FF_LINEMARK:
  221.         linemark = t;
  222.         lines++;
  223.         gotsome = FALSE;
  224.         break;
  225.  
  226.     case FF_LITERAL:
  227.         arg = *fpc++;
  228.         while (arg--)
  229.         *t++ = *f++;
  230.         break;
  231.  
  232.     case FF_SKIP:
  233.         f += *fpc++;
  234.         break;
  235.  
  236.     case FF_FETCH:
  237.         arg = *fpc++;
  238.         f += arg;
  239.         fieldsize = arg;
  240.  
  241.         if (MARK < SP)
  242.         sv = *++MARK;
  243.         else {
  244.         sv = &sv_no;
  245.         if (dowarn)
  246.             warn("Not enough format arguments");
  247.         }
  248.         break;
  249.  
  250.     case FF_CHECKNL:
  251.         item = s = SvPV(sv, len);
  252.         itemsize = len;
  253.         if (itemsize > fieldsize)
  254.         itemsize = fieldsize;
  255.         send = chophere = s + itemsize;
  256.         while (s < send) {
  257.         if (*s & ~31)
  258.             gotsome = TRUE;
  259.         else if (*s == '\n')
  260.             break;
  261.         s++;
  262.         }
  263.         itemsize = s - item;
  264.         break;
  265.  
  266.     case FF_CHECKCHOP:
  267.         item = s = SvPV(sv, len);
  268.         itemsize = len;
  269.         if (itemsize <= fieldsize) {
  270.         send = chophere = s + itemsize;
  271.         while (s < send) {
  272.             if (*s == '\r') {
  273.             itemsize = s - item;
  274.             break;
  275.             }
  276.             if (*s++ & ~31)
  277.             gotsome = TRUE;
  278.         }
  279.         }
  280.         else {
  281.         itemsize = fieldsize;
  282.         send = chophere = s + itemsize;
  283.         while (s < send || (s == send && isSPACE(*s))) {
  284.             if (isSPACE(*s)) {
  285.             if (chopspace)
  286.                 chophere = s;
  287.             if (*s == '\r')
  288.                 break;
  289.             }
  290.             else {
  291.             if (*s & ~31)
  292.                 gotsome = TRUE;
  293.             if (strchr(chopset, *s))
  294.                 chophere = s + 1;
  295.             }
  296.             s++;
  297.         }
  298.         itemsize = chophere - item;
  299.         }
  300.         break;
  301.  
  302.     case FF_SPACE:
  303.         arg = fieldsize - itemsize;
  304.         if (arg) {
  305.         fieldsize -= arg;
  306.         while (arg-- > 0)
  307.             *t++ = ' ';
  308.         }
  309.         break;
  310.  
  311.     case FF_HALFSPACE:
  312.         arg = fieldsize - itemsize;
  313.         if (arg) {
  314.         arg /= 2;
  315.         fieldsize -= arg;
  316.         while (arg-- > 0)
  317.             *t++ = ' ';
  318.         }
  319.         break;
  320.  
  321.     case FF_ITEM:
  322.         arg = itemsize;
  323.         s = item;
  324.         while (arg--) {
  325. #if 'z' - 'a' != 25
  326.         int ch = *t++ = *s++;
  327.         if (!iscntrl(ch))
  328.             t[-1] = ' ';
  329. #else
  330.         if ( !((*t++ = *s++) & ~31) )
  331.             t[-1] = ' ';
  332. #endif
  333.  
  334.         }
  335.         break;
  336.  
  337.     case FF_CHOP:
  338.         s = chophere;
  339.         if (chopspace) {
  340.         while (*s && isSPACE(*s))
  341.             s++;
  342.         }
  343.         sv_chop(sv,s);
  344.         break;
  345.  
  346.     case FF_LINEGLOB:
  347.         item = s = SvPV(sv, len);
  348.         itemsize = len;
  349.         if (itemsize) {
  350.         gotsome = TRUE;
  351.         send = s + itemsize;
  352.         while (s < send) {
  353.             if (*s++ == '\n') {
  354.             if (s == send)
  355.                 itemsize--;
  356.             else
  357.                 lines++;
  358.             }
  359.         }
  360.         SvCUR_set(formtarget, t - SvPVX(formtarget));
  361.         sv_catpvn(formtarget, item, itemsize);
  362.         SvGROW(formtarget, SvCUR(formtarget) + SvCUR(form) + 1);
  363.         t = SvPVX(formtarget) + SvCUR(formtarget);
  364.         }
  365.         break;
  366.  
  367.     case FF_DECIMAL:
  368.         /* If the field is marked with ^ and the value is undefined,
  369.            blank it out. */
  370.         arg = *fpc++;
  371.         if ((arg & 512) && !SvOK(sv)) {
  372.         arg = fieldsize;
  373.         while (arg--)
  374.             *t++ = ' ';
  375.         break;
  376.         }
  377.         gotsome = TRUE;
  378.         value = SvNV(sv);
  379.         if (arg & 256) {
  380.         sprintf(t, "%#*.*f", (int) fieldsize, (int) arg & 255, value);
  381.         } else {
  382.         sprintf(t, "%*.0f", (int) fieldsize, value);
  383.         }
  384.         t += fieldsize;
  385.         break;
  386.  
  387.     case FF_NEWLINE:
  388.         f++;
  389.         while (t-- > linemark && *t == ' ') ;
  390.         t++;
  391.         *t++ = '\n';
  392.         break;
  393.  
  394.     case FF_BLANK:
  395.         arg = *fpc++;
  396.         if (gotsome) {
  397.         if (arg) {        /* repeat until fields exhausted? */
  398.             *t = '\0';
  399.             SvCUR_set(formtarget, t - SvPVX(formtarget));
  400.             lines += FmLINES(formtarget);
  401.             if (lines == 200) {
  402.             arg = t - linemark;
  403.             if (strnEQ(linemark, linemark - arg, arg))
  404.                 DIE("Runaway format");
  405.             }
  406.             FmLINES(formtarget) = lines;
  407.             SP = ORIGMARK;
  408.             RETURNOP(cLISTOP->op_first);
  409.         }
  410.         }
  411.         else {
  412.         t = linemark;
  413.         lines--;
  414.         }
  415.         break;
  416.  
  417.     case FF_MORE:
  418.         if (itemsize) {
  419.         arg = fieldsize - itemsize;
  420.         if (arg) {
  421.             fieldsize -= arg;
  422.             while (arg-- > 0)
  423.             *t++ = ' ';
  424.         }
  425.         s = t - 3;
  426.         if (strnEQ(s,"   ",3)) {
  427.             while (s > SvPVX(formtarget) && isSPACE(s[-1]))
  428.             s--;
  429.         }
  430.         *s++ = '.';
  431.         *s++ = '.';
  432.         *s++ = '.';
  433.         }
  434.         break;
  435.  
  436.     case FF_END:
  437.         *t = '\0';
  438.         SvCUR_set(formtarget, t - SvPVX(formtarget));
  439.         FmLINES(formtarget) += lines;
  440.         SP = ORIGMARK;
  441.         RETPUSHYES;
  442.     }
  443.     }
  444. }
  445.  
  446. PP(pp_grepstart)
  447. {
  448.     dSP;
  449.     SV *src;
  450.  
  451.     if (stack_base + *markstack_ptr == sp) {
  452.     (void)POPMARK;
  453.     if (GIMME != G_ARRAY)
  454.         XPUSHs(&sv_no);
  455.     RETURNOP(op->op_next->op_next);
  456.     }
  457.     stack_sp = stack_base + *markstack_ptr + 1;
  458.     pp_pushmark();                /* push dst */
  459.     pp_pushmark();                /* push src */
  460.     ENTER;                    /* enter outer scope */
  461.  
  462.     SAVETMPS;
  463.     SAVESPTR(GvSV(defgv));
  464.  
  465.     ENTER;                    /* enter inner scope */
  466.     SAVESPTR(curpm);
  467.  
  468.     src = stack_base[*markstack_ptr];
  469.     SvTEMP_off(src);
  470.     GvSV(defgv) = src;
  471.  
  472.     PUTBACK;
  473.     if (op->op_type == OP_MAPSTART)
  474.     pp_pushmark();                /* push top */
  475.     return ((LOGOP*)op->op_next)->op_other;
  476. }
  477.  
  478. PP(pp_mapstart)
  479. {
  480.     DIE("panic: mapstart");    /* uses grepstart */
  481. }
  482.  
  483. PP(pp_mapwhile)
  484. {
  485.     dSP;
  486.     I32 diff = (sp - stack_base) - *markstack_ptr;
  487.     I32 count;
  488.     I32 shift;
  489.     SV** src;
  490.     SV** dst; 
  491.  
  492.     ++markstack_ptr[-1];
  493.     if (diff) {
  494.     if (diff > markstack_ptr[-1] - markstack_ptr[-2]) {
  495.         shift = diff - (markstack_ptr[-1] - markstack_ptr[-2]);
  496.         count = (sp - stack_base) - markstack_ptr[-1] + 2;
  497.         
  498.         EXTEND(sp,shift);
  499.         src = sp;
  500.         dst = (sp += shift);
  501.         markstack_ptr[-1] += shift;
  502.         *markstack_ptr += shift;
  503.         while (--count)
  504.         *dst-- = *src--;
  505.     }
  506.     dst = stack_base + (markstack_ptr[-2] += diff) - 1; 
  507.     ++diff;
  508.     while (--diff)
  509.         *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs); 
  510.     }
  511.     LEAVE;                    /* exit inner scope */
  512.  
  513.     /* All done yet? */
  514.     if (markstack_ptr[-1] > *markstack_ptr) {
  515.     I32 items;
  516.  
  517.     (void)POPMARK;                /* pop top */
  518.     LEAVE;                    /* exit outer scope */
  519.     (void)POPMARK;                /* pop src */
  520.     items = --*markstack_ptr - markstack_ptr[-1];
  521.     (void)POPMARK;                /* pop dst */
  522.     SP = stack_base + POPMARK;        /* pop original mark */
  523.     if (GIMME != G_ARRAY) {
  524.         dTARGET;
  525.         XPUSHi(items);
  526.         RETURN;
  527.     }
  528.     SP += items;
  529.     RETURN;
  530.     }
  531.     else {
  532.     SV *src;
  533.  
  534.     ENTER;                    /* enter inner scope */
  535.     SAVESPTR(curpm);
  536.  
  537.     src = stack_base[markstack_ptr[-1]];
  538.     SvTEMP_off(src);
  539.     GvSV(defgv) = src;
  540.  
  541.     RETURNOP(cLOGOP->op_other);
  542.     }
  543. }
  544.  
  545.  
  546. PP(pp_sort)
  547. {
  548.     dSP; dMARK; dORIGMARK;
  549.     register SV **up;
  550.     SV **myorigmark = ORIGMARK;
  551.     register I32 max;
  552.     HV *stash;
  553.     GV *gv;
  554.     CV *cv;
  555.     I32 gimme = GIMME;
  556.     OP* nextop = op->op_next;
  557.  
  558.     if (gimme != G_ARRAY) {
  559.     SP = MARK;
  560.     RETPUSHUNDEF;
  561.     }
  562.  
  563.     if (op->op_flags & OPf_STACKED) {
  564.     ENTER;
  565.     if (op->op_flags & OPf_SPECIAL) {
  566.         OP *kid = cLISTOP->op_first->op_sibling;    /* pass pushmark */
  567.         kid = kUNOP->op_first;            /* pass rv2gv */
  568.         kid = kUNOP->op_first;            /* pass leave */
  569.         sortcop = kid->op_next;
  570.         stash = curcop->cop_stash;
  571.     }
  572.     else {
  573.         cv = sv_2cv(*++MARK, &stash, &gv, 0);
  574.         if (!(cv && CvROOT(cv))) {
  575.         if (gv) {
  576.             SV *tmpstr = sv_newmortal();
  577.             gv_efullname(tmpstr, gv);
  578.             if (cv && CvXSUB(cv))
  579.             DIE("Xsub \"%s\" called in sort", SvPVX(tmpstr));
  580.             DIE("Undefined sort subroutine \"%s\" called",
  581.             SvPVX(tmpstr));
  582.         }
  583.         if (cv) {
  584.             if (CvXSUB(cv))
  585.             DIE("Xsub called in sort");
  586.             DIE("Undefined subroutine in sort");
  587.         }
  588.         DIE("Not a CODE reference in sort");
  589.         }
  590.         sortcop = CvSTART(cv);
  591.         SAVESPTR(CvROOT(cv)->op_ppaddr);
  592.         CvROOT(cv)->op_ppaddr = ppaddr[OP_NULL];
  593.         
  594.         SAVESPTR(curpad);
  595.         curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
  596.     }
  597.     }
  598.     else {
  599.     sortcop = Nullop;
  600.     stash = curcop->cop_stash;
  601.     }
  602.  
  603.     up = myorigmark + 1;
  604.     while (MARK < SP) {    /* This may or may not shift down one here. */
  605.     /*SUPPRESS 560*/
  606.     if (*up = *++MARK) {            /* Weed out nulls. */
  607.         if (!SvPOK(*up))
  608.         (void)sv_2pv(*up, &na);
  609.         else
  610.         SvTEMP_off(*up);
  611.         up++;
  612.     }
  613.     }
  614.     max = --up - myorigmark;
  615.     if (sortcop) {
  616.     if (max > 1) {
  617.         AV *oldstack;
  618.         CONTEXT *cx;
  619.         SV** newsp;
  620.  
  621.         SAVETMPS;
  622.         SAVESPTR(op);
  623.  
  624.         oldstack = stack;
  625.         if (!sortstack) {
  626.         sortstack = newAV();
  627.         AvREAL_off(sortstack);
  628.         av_extend(sortstack, 32);
  629.         }
  630.         SWITCHSTACK(stack, sortstack);
  631.         if (sortstash != stash) {
  632.         firstgv = gv_fetchpv("a", TRUE, SVt_PV);
  633.         secondgv = gv_fetchpv("b", TRUE, SVt_PV);
  634.         sortstash = stash;
  635.         }
  636.  
  637.         SAVESPTR(GvSV(firstgv));
  638.         SAVESPTR(GvSV(secondgv));
  639.         PUSHBLOCK(cx, CXt_LOOP, stack_base);
  640.         sortcxix = cxstack_ix;
  641.  
  642.         qsort((char*)(myorigmark+1), max, sizeof(SV*), sortcv);
  643.  
  644.         POPBLOCK(cx,curpm);
  645.         SWITCHSTACK(sortstack, oldstack);
  646.     }
  647.     LEAVE;
  648.     }
  649.     else {
  650.     if (max > 1) {
  651.         MEXTEND(SP, 20);    /* Can't afford stack realloc on signal. */
  652.         qsort((char*)(ORIGMARK+1), max, sizeof(SV*), sortcmp);
  653.     }
  654.     }
  655.     stack_sp = ORIGMARK + max;
  656.     return nextop;
  657. }
  658.  
  659. /* Range stuff. */
  660.  
  661. PP(pp_range)
  662. {
  663.     if (GIMME == G_ARRAY)
  664.     return cCONDOP->op_true;
  665.     return SvTRUEx(PAD_SV(op->op_targ)) ? cCONDOP->op_false : cCONDOP->op_true;
  666. }
  667.  
  668. PP(pp_flip)
  669. {
  670.     dSP;
  671.  
  672.     if (GIMME == G_ARRAY) {
  673.     RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
  674.     }
  675.     else {
  676.     dTOPss;
  677.     SV *targ = PAD_SV(op->op_targ);
  678.  
  679.     if ((op->op_private & OPpFLIP_LINENUM)
  680.       ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
  681.       : SvTRUE(sv) ) {
  682.         sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
  683.         if (op->op_flags & OPf_SPECIAL) {
  684.         sv_setiv(targ, 1);
  685.         RETURN;
  686.         }
  687.         else {
  688.         sv_setiv(targ, 0);
  689.         sp--;
  690.         RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
  691.         }
  692.     }
  693.     sv_setpv(TARG, "");
  694.     SETs(targ);
  695.     RETURN;
  696.     }
  697. }
  698.  
  699. PP(pp_flop)
  700. {
  701.     dSP;
  702.  
  703.     if (GIMME == G_ARRAY) {
  704.     dPOPPOPssrl;
  705.     register I32 i;
  706.     register SV *sv;
  707.     I32 max;
  708.  
  709.     if (SvNIOKp(left) || !SvPOKp(left) ||
  710.       (looks_like_number(left) && *SvPVX(left) != '0') ) {
  711.         i = SvIV(left);
  712.         max = SvIV(right);
  713.         if (max > i)
  714.         EXTEND(SP, max - i + 1);
  715.         while (i <= max) {
  716.         sv = sv_mortalcopy(&sv_no);
  717.         sv_setiv(sv,i++);
  718.         PUSHs(sv);
  719.         }
  720.     }
  721.     else {
  722.         SV *final = sv_mortalcopy(right);
  723.         STRLEN len;
  724.         char *tmps = SvPV(final, len);
  725.  
  726.         sv = sv_mortalcopy(left);
  727.         while (!SvNIOKp(sv) && SvCUR(sv) <= len &&
  728.         strNE(SvPVX(sv),tmps) ) {
  729.         XPUSHs(sv);
  730.         sv = sv_2mortal(newSVsv(sv));
  731.         sv_inc(sv);
  732.         }
  733.         if (strEQ(SvPVX(sv),tmps))
  734.         XPUSHs(sv);
  735.     }
  736.     }
  737.     else {
  738.     dTOPss;
  739.     SV *targ = PAD_SV(cUNOP->op_first->op_targ);
  740.     sv_inc(targ);
  741.     if ((op->op_private & OPpFLIP_LINENUM)
  742.       ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
  743.       : SvTRUE(sv) ) {
  744.         sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
  745.         sv_catpv(targ, "E0");
  746.     }
  747.     SETs(targ);
  748.     }
  749.  
  750.     RETURN;
  751. }
  752.  
  753. /* Control. */
  754.  
  755. static I32
  756. dopoptolabel(label)
  757. char *label;
  758. {
  759.     register I32 i;
  760.     register CONTEXT *cx;
  761.  
  762.     for (i = cxstack_ix; i >= 0; i--) {
  763.     cx = &cxstack[i];
  764.     switch (cx->cx_type) {
  765.     case CXt_SUBST:
  766.         if (dowarn)
  767.         warn("Exiting substitution via %s", op_name[op->op_type]);
  768.         break;
  769.     case CXt_SUB:
  770.         if (dowarn)
  771.         warn("Exiting subroutine via %s", op_name[op->op_type]);
  772.         break;
  773.     case CXt_EVAL:
  774.         if (dowarn)
  775.         warn("Exiting eval via %s", op_name[op->op_type]);
  776.         break;
  777.     case CXt_LOOP:
  778.         if (!cx->blk_loop.label ||
  779.           strNE(label, cx->blk_loop.label) ) {
  780.         DEBUG_l(deb("(Skipping label #%d %s)\n",
  781.             i, cx->blk_loop.label));
  782.         continue;
  783.         }
  784.         DEBUG_l( deb("(Found label #%d %s)\n", i, label));
  785.         return i;
  786.     }
  787.     }
  788.     return i;
  789. }
  790.  
  791. I32
  792. dowantarray()
  793. {
  794.     I32 cxix;
  795.  
  796.     cxix = dopoptosub(cxstack_ix);
  797.     if (cxix < 0)
  798.     return G_SCALAR;
  799.  
  800.     if (cxstack[cxix].blk_gimme == G_ARRAY)
  801.     return G_ARRAY;
  802.     else
  803.     return G_SCALAR;
  804. }
  805.  
  806. static I32
  807. dopoptosub(startingblock)
  808. I32 startingblock;
  809. {
  810.     I32 i;
  811.     register CONTEXT *cx;
  812.     for (i = startingblock; i >= 0; i--) {
  813.     cx = &cxstack[i];
  814.     switch (cx->cx_type) {
  815.     default:
  816.         continue;
  817.     case CXt_EVAL:
  818.     case CXt_SUB:
  819.         DEBUG_l( deb("(Found sub #%d)\n", i));
  820.         return i;
  821.     }
  822.     }
  823.     return i;
  824. }
  825.  
  826. static I32
  827. dopoptoeval(startingblock)
  828. I32 startingblock;
  829. {
  830.     I32 i;
  831.     register CONTEXT *cx;
  832.     for (i = startingblock; i >= 0; i--) {
  833.     cx = &cxstack[i];
  834.     switch (cx->cx_type) {
  835.     default:
  836.         continue;
  837.     case CXt_EVAL:
  838.         DEBUG_l( deb("(Found eval #%d)\n", i));
  839.         return i;
  840.     }
  841.     }
  842.     return i;
  843. }
  844.  
  845. static I32
  846. dopoptoloop(startingblock)
  847. I32 startingblock;
  848. {
  849.     I32 i;
  850.     register CONTEXT *cx;
  851.     for (i = startingblock; i >= 0; i--) {
  852.     cx = &cxstack[i];
  853.     switch (cx->cx_type) {
  854.     case CXt_SUBST:
  855.         if (dowarn)
  856.         warn("Exiting substitition via %s", op_name[op->op_type]);
  857.         break;
  858.     case CXt_SUB:
  859.         if (dowarn)
  860.         warn("Exiting subroutine via %s", op_name[op->op_type]);
  861.         break;
  862.     case CXt_EVAL:
  863.         if (dowarn)
  864.         warn("Exiting eval via %s", op_name[op->op_type]);
  865.         break;
  866.     case CXt_LOOP:
  867.         DEBUG_l( deb("(Found loop #%d)\n", i));
  868.         return i;
  869.     }
  870.     }
  871.     return i;
  872. }
  873.  
  874. void
  875. dounwind(cxix)
  876. I32 cxix;
  877. {
  878.     register CONTEXT *cx;
  879.     SV **newsp;
  880.     I32 optype;
  881.  
  882.     while (cxstack_ix > cxix) {
  883.     cx = &cxstack[cxstack_ix--];
  884.     DEBUG_l(fprintf(stderr, "Unwinding block %ld, type %s\n", (long) cxstack_ix+1,
  885.             block_type[cx->cx_type]));
  886.     /* Note: we don't need to restore the base context info till the end. */
  887.     switch (cx->cx_type) {
  888.     case CXt_SUB:
  889.         POPSUB(cx);
  890.         break;
  891.     case CXt_EVAL:
  892.         POPEVAL(cx);
  893.         break;
  894.     case CXt_LOOP:
  895.         POPLOOP(cx);
  896.         break;
  897.     case CXt_SUBST:
  898.         break;
  899.     }
  900.     }
  901. }
  902.  
  903. #ifdef I_STDARG
  904. OP *
  905. die(char* pat, ...)
  906. #else
  907. /*VARARGS0*/
  908. OP *
  909. die(pat, va_alist)
  910.     char *pat;
  911.     va_dcl
  912. #endif
  913. {
  914.     va_list args;
  915.     char *message;
  916.     int oldrunlevel = runlevel;
  917.     int was_in_eval = in_eval;
  918.     HV *stash;
  919.     GV *gv;
  920.     CV *cv;
  921.  
  922. #ifdef I_STDARG
  923.     va_start(args, pat);
  924. #else
  925.     va_start(args);
  926. #endif
  927.     message = mess(pat, &args);
  928.     va_end(args);
  929.     if (diehook && (cv = sv_2cv(diehook, &stash, &gv, 0)) && !CvDEPTH(cv)) {
  930.     dSP;
  931.  
  932.     PUSHMARK(sp);
  933.     EXTEND(sp, 1);
  934.     PUSHs(sv_2mortal(newSVpv(message,0)));
  935.     PUTBACK;
  936.     perl_call_sv((SV*)cv, G_DISCARD);
  937.     }
  938.     restartop = die_where(message);
  939.     if ((!restartop && was_in_eval) || oldrunlevel > 1)
  940.     Siglongjmp(top_env, 3);
  941.     return restartop;
  942. }
  943.  
  944. OP *
  945. die_where(message)
  946. char *message;
  947. {
  948.     if (in_eval) {
  949.     I32 cxix;
  950.     register CONTEXT *cx;
  951.     I32 gimme;
  952.     SV **newsp;
  953.  
  954.     if (in_eval & 4) {
  955.         SV **svp;
  956.         STRLEN klen = strlen(message);
  957.         
  958.         svp = hv_fetch(GvHV(errgv), message, klen, TRUE);
  959.         if (svp) {
  960.         if (!SvIOK(*svp)) {
  961.             static char prefix[] = "\t(in cleanup) ";
  962.             sv_upgrade(*svp, SVt_IV);
  963.             (void)SvIOK_only(*svp);
  964.             SvGROW(GvSV(errgv), SvCUR(GvSV(errgv))+sizeof(prefix)+klen);
  965.             sv_catpvn(GvSV(errgv), prefix, sizeof(prefix)-1);
  966.             sv_catpvn(GvSV(errgv), message, klen);
  967.         }
  968.         sv_inc(*svp);
  969.         }
  970.     }
  971.     else
  972.         sv_setpv(GvSV(errgv), message);
  973.     
  974.     cxix = dopoptoeval(cxstack_ix);
  975.     if (cxix >= 0) {
  976.         I32 optype;
  977.  
  978.         if (cxix < cxstack_ix)
  979.         dounwind(cxix);
  980.  
  981.         POPBLOCK(cx,curpm);
  982.         if (cx->cx_type != CXt_EVAL) {
  983.         fprintf(stderr, "panic: die %s", message);
  984.         my_exit(1);
  985.         }
  986.         POPEVAL(cx);
  987.  
  988.         if (gimme == G_SCALAR)
  989.         *++newsp = &sv_undef;
  990.         stack_sp = newsp;
  991.  
  992.         LEAVE;
  993.  
  994.         if (optype == OP_REQUIRE)
  995.         DIE("%s", SvPVx(GvSV(errgv), na));
  996.         return pop_return();
  997.     }
  998.     }
  999.     fputs(message, stderr);
  1000.     (void)Fflush(stderr);
  1001.     if (e_fp) {
  1002.     fclose(e_fp);
  1003.     e_fp = Nullfp;
  1004.     (void)UNLINK(e_tmpname);
  1005.     }
  1006.     statusvalue = SHIFTSTATUS(statusvalue);
  1007. #ifdef VMS
  1008.     my_exit((U32)vaxc$errno?vaxc$errno:errno?errno:statusvalue?statusvalue:SS$_ABORT);
  1009. #else
  1010.     my_exit((I32)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
  1011. #endif
  1012.     return 0;
  1013. }
  1014.  
  1015. PP(pp_xor)
  1016. {
  1017.     dSP; dPOPTOPssrl;
  1018.     if (SvTRUE(left) != SvTRUE(right))
  1019.     RETSETYES;
  1020.     else
  1021.     RETSETNO;
  1022. }
  1023.  
  1024. PP(pp_andassign)
  1025. {
  1026.     dSP;
  1027.     if (!SvTRUE(TOPs))
  1028.     RETURN;
  1029.     else
  1030.     RETURNOP(cLOGOP->op_other);
  1031. }
  1032.  
  1033. PP(pp_orassign)
  1034. {
  1035.     dSP;
  1036.     if (SvTRUE(TOPs))
  1037.     RETURN;
  1038.     else
  1039.     RETURNOP(cLOGOP->op_other);
  1040. }
  1041.     
  1042. #ifdef DEPRECATED
  1043. PP(pp_entersubr)
  1044. {
  1045.     dSP;
  1046.     SV** mark = (stack_base + *markstack_ptr + 1);
  1047.     SV* cv = *mark;
  1048.     while (mark < sp) {    /* emulate old interface */
  1049.     *mark = mark[1];
  1050.     mark++;
  1051.     }
  1052.     *sp = cv;
  1053.     return pp_entersub();
  1054. }
  1055. #endif
  1056.  
  1057. PP(pp_caller)
  1058. {
  1059.     dSP;
  1060.     register I32 cxix = dopoptosub(cxstack_ix);
  1061.     register CONTEXT *cx;
  1062.     I32 dbcxix;
  1063.     SV *sv;
  1064.     I32 count = 0;
  1065.  
  1066.     if (MAXARG)
  1067.     count = POPi;
  1068.     EXTEND(SP, 6);
  1069.     for (;;) {
  1070.     if (cxix < 0) {
  1071.         if (GIMME != G_ARRAY)
  1072.         RETPUSHUNDEF;
  1073.         RETURN;
  1074.     }
  1075.     if (DBsub && cxix >= 0 &&
  1076.         cxstack[cxix].blk_sub.cv == GvCV(DBsub))
  1077.         count++;
  1078.     if (!count--)
  1079.         break;
  1080.     cxix = dopoptosub(cxix - 1);
  1081.     }
  1082.     cx = &cxstack[cxix];
  1083.     if (cxstack[cxix].cx_type == CXt_SUB) {
  1084.         dbcxix = dopoptosub(cxix - 1);
  1085.     /* We expect that cxstack[dbcxix] is CXt_SUB, anyway, the
  1086.        field below is defined for any cx. */
  1087.     if (DBsub && dbcxix >= 0 && cxstack[dbcxix].blk_sub.cv == GvCV(DBsub))
  1088.         cx = &cxstack[dbcxix];
  1089.     }
  1090.  
  1091.     if (GIMME != G_ARRAY) {
  1092.     dTARGET;
  1093.  
  1094.     sv_setpv(TARG, HvNAME(cx->blk_oldcop->cop_stash));
  1095.     PUSHs(TARG);
  1096.     RETURN;
  1097.     }
  1098.  
  1099.     PUSHs(sv_2mortal(newSVpv(HvNAME(cx->blk_oldcop->cop_stash), 0)));
  1100.     PUSHs(sv_2mortal(newSVpv(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)), 0)));
  1101.     PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
  1102.     if (!MAXARG)
  1103.     RETURN;
  1104.     if (cx->cx_type == CXt_SUB) { /* So is cxstack[dbcxix]. */
  1105.     sv = NEWSV(49, 0);
  1106.     gv_efullname(sv, CvGV(cxstack[cxix].blk_sub.cv));
  1107.     PUSHs(sv_2mortal(sv));
  1108.     PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
  1109.     }
  1110.     else {
  1111.     PUSHs(sv_2mortal(newSVpv("(eval)",0)));
  1112.     PUSHs(sv_2mortal(newSViv(0)));
  1113.     }
  1114.     PUSHs(sv_2mortal(newSViv((I32)cx->blk_gimme)));
  1115.     if (cx->cx_type == CXt_EVAL) {
  1116.     if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
  1117.         PUSHs(cx->blk_eval.cur_text);
  1118.         PUSHs(&sv_no);
  1119.     } 
  1120.     else if (cx->blk_eval.old_name) { /* Try blocks have old_name == 0. */
  1121.         /* Require, put the name. */
  1122.         PUSHs(sv_2mortal(newSVpv(cx->blk_eval.old_name, 0)));
  1123.         PUSHs(&sv_yes);
  1124.     }
  1125.     }
  1126.     else if (cx->cx_type == CXt_SUB &&
  1127.         cx->blk_sub.hasargs &&
  1128.         curcop->cop_stash == debstash)
  1129.     {
  1130.     AV *ary = cx->blk_sub.argarray;
  1131.     int off = AvARRAY(ary) - AvALLOC(ary);
  1132.  
  1133.     if (!dbargs) {
  1134.         GV* tmpgv;
  1135.         dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
  1136.                 SVt_PVAV)));
  1137.         GvMULTI_on(tmpgv);
  1138.         AvREAL_off(dbargs);        /* XXX Should be REIFY */
  1139.     }
  1140.  
  1141.     if (AvMAX(dbargs) < AvFILL(ary) + off)
  1142.         av_extend(dbargs, AvFILL(ary) + off);
  1143.     Copy(AvALLOC(ary), AvARRAY(dbargs), AvFILL(ary) + 1 + off, SV*);
  1144.     AvFILL(dbargs) = AvFILL(ary) + off;
  1145.     }
  1146.     RETURN;
  1147. }
  1148.  
  1149. static int
  1150. sortcv(a, b)
  1151. const void *a;
  1152. const void *b;
  1153. {
  1154.     SV **str1 = (SV **) a;
  1155.     SV **str2 = (SV **) b;
  1156.     I32 oldsaveix = savestack_ix;
  1157.     I32 oldscopeix = scopestack_ix;
  1158.     I32 result;
  1159.     GvSV(firstgv) = *str1;
  1160.     GvSV(secondgv) = *str2;
  1161.     stack_sp = stack_base;
  1162.     op = sortcop;
  1163.     run();
  1164.     if (stack_sp != stack_base + 1)
  1165.     croak("Sort subroutine didn't return single value");
  1166.     if (!SvNIOKp(*stack_sp))
  1167.     croak("Sort subroutine didn't return a numeric value");
  1168.     result = SvIV(*stack_sp);
  1169.     while (scopestack_ix > oldscopeix) {
  1170.     LEAVE;
  1171.     }
  1172.     leave_scope(oldsaveix);
  1173.     return result;
  1174. }
  1175.  
  1176. static int
  1177. sortcmp(a, b)
  1178. const void *a;
  1179. const void *b;
  1180. {
  1181.     register SV *str1 = *(SV **) a;
  1182.     register SV *str2 = *(SV **) b;
  1183.     I32 retval;
  1184.  
  1185.     if (!SvPOKp(str1)) {
  1186.     if (!SvPOKp(str2))
  1187.         return 0;
  1188.     else
  1189.         return -1;
  1190.     }
  1191.     if (!SvPOKp(str2))
  1192.     return 1;
  1193.  
  1194.     if (SvCUR(str1) < SvCUR(str2)) {
  1195.     /*SUPPRESS 560*/
  1196.     if (retval = memcmp(SvPVX(str1), SvPVX(str2), SvCUR(str1)))
  1197.         return retval;
  1198.     else
  1199.         return -1;
  1200.     }
  1201.     /*SUPPRESS 560*/
  1202.     else if (retval = memcmp(SvPVX(str1), SvPVX(str2), SvCUR(str2)))
  1203.     return retval;
  1204.     else if (SvCUR(str1) == SvCUR(str2))
  1205.     return 0;
  1206.     else
  1207.     return 1;
  1208. }
  1209.  
  1210. PP(pp_reset)
  1211. {
  1212.     dSP;
  1213.     char *tmps;
  1214.  
  1215.     if (MAXARG < 1)
  1216.     tmps = "";
  1217.     else
  1218.     tmps = POPp;
  1219.     sv_reset(tmps, curcop->cop_stash);
  1220.     PUSHs(&sv_yes);
  1221.     RETURN;
  1222. }
  1223.  
  1224. PP(pp_lineseq)
  1225. {
  1226.     return NORMAL;
  1227. }
  1228.  
  1229. PP(pp_dbstate)
  1230. {
  1231.     curcop = (COP*)op;
  1232.     TAINT_NOT;        /* Each statement is presumed innocent */
  1233.     stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
  1234.     FREETMPS;
  1235.  
  1236.     if (op->op_private || SvIV(DBsingle) || SvIV(DBsignal) || SvIV(DBtrace))
  1237.     {
  1238.     SV **sp;
  1239.     register CV *cv;
  1240.     register CONTEXT *cx;
  1241.     I32 gimme = G_ARRAY;
  1242.     I32 hasargs;
  1243.     GV *gv;
  1244.  
  1245.     gv = DBgv;
  1246.     cv = GvCV(gv);
  1247.     if (!cv)
  1248.         DIE("No DB::DB routine defined");
  1249.  
  1250.     if (CvDEPTH(cv) >= 1 && !(debug & (1<<30))) /* don't do recursive DB::DB call */
  1251.         return NORMAL;
  1252.  
  1253.     ENTER;
  1254.     SAVETMPS;
  1255.  
  1256.     SAVEI32(debug);
  1257.     SAVESPTR(stack_sp);
  1258.     debug = 0;
  1259.     hasargs = 0;
  1260.     sp = stack_sp;
  1261.  
  1262.     push_return(op->op_next);
  1263.     PUSHBLOCK(cx, CXt_SUB, sp);
  1264.     PUSHSUB(cx);
  1265.     CvDEPTH(cv)++;
  1266.     (void)SvREFCNT_inc(cv);
  1267.     SAVESPTR(curpad);
  1268.     curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
  1269.     RETURNOP(CvSTART(cv));
  1270.     }
  1271.     else
  1272.     return NORMAL;
  1273. }
  1274.  
  1275. PP(pp_scope)
  1276. {
  1277.     return NORMAL;
  1278. }
  1279.  
  1280. PP(pp_enteriter)
  1281. {
  1282.     dSP; dMARK;
  1283.     register CONTEXT *cx;
  1284.     I32 gimme = GIMME;
  1285.     SV **svp;
  1286.  
  1287.     ENTER;
  1288.     SAVETMPS;
  1289.  
  1290.     if (op->op_targ)
  1291.     svp = &curpad[op->op_targ];        /* "my" variable */
  1292.     else
  1293.     svp = &GvSV((GV*)POPs);            /* symbol table variable */
  1294.  
  1295.     SAVESPTR(*svp);
  1296.  
  1297.     ENTER;
  1298.  
  1299.     PUSHBLOCK(cx, CXt_LOOP, SP);
  1300.     PUSHLOOP(cx, svp, MARK);
  1301.     if (op->op_flags & OPf_STACKED) {
  1302.     AV* av = (AV*)POPs;
  1303.     cx->blk_loop.iterary = av;
  1304.     cx->blk_loop.iterix = -1;
  1305.     }
  1306.     else {
  1307.     cx->blk_loop.iterary = stack;
  1308.     AvFILL(stack) = sp - stack_base;
  1309.     cx->blk_loop.iterix = MARK - stack_base;
  1310.     }
  1311.  
  1312.     RETURN;
  1313. }
  1314.  
  1315. PP(pp_enterloop)
  1316. {
  1317.     dSP;
  1318.     register CONTEXT *cx;
  1319.     I32 gimme = GIMME;
  1320.  
  1321.     ENTER;
  1322.     SAVETMPS;
  1323.     ENTER;
  1324.  
  1325.     PUSHBLOCK(cx, CXt_LOOP, SP);
  1326.     PUSHLOOP(cx, 0, SP);
  1327.  
  1328.     RETURN;
  1329. }
  1330.  
  1331. PP(pp_leaveloop)
  1332. {
  1333.     dSP;
  1334.     register CONTEXT *cx;
  1335.     I32 gimme;
  1336.     SV **newsp;
  1337.     PMOP *newpm;
  1338.     SV **mark;
  1339.  
  1340.     POPBLOCK(cx,newpm);
  1341.     mark = newsp;
  1342.     POPLOOP(cx);
  1343.     if (gimme == G_SCALAR) {
  1344.     if (op->op_private & OPpLEAVE_VOID)
  1345.         ;
  1346.     else {
  1347.         if (mark < SP)
  1348.         *++newsp = sv_mortalcopy(*SP);
  1349.         else
  1350.         *++newsp = &sv_undef;
  1351.     }
  1352.     }
  1353.     else {
  1354.     while (mark < SP)
  1355.         *++newsp = sv_mortalcopy(*++mark);
  1356.     }
  1357.     curpm = newpm;    /* Don't pop $1 et al till now */
  1358.     sp = newsp;
  1359.     LEAVE;
  1360.     LEAVE;
  1361.  
  1362.     RETURN;
  1363. }
  1364.  
  1365. PP(pp_return)
  1366. {
  1367.     dSP; dMARK;
  1368.     I32 cxix;
  1369.     register CONTEXT *cx;
  1370.     I32 gimme;
  1371.     SV **newsp;
  1372.     PMOP *newpm;
  1373.     I32 optype = 0;
  1374.  
  1375.     if (stack == sortstack) {
  1376.     if (cxstack_ix == sortcxix || dopoptosub(cxstack_ix) < sortcxix) {
  1377.         if (cxstack_ix > sortcxix)
  1378.         dounwind(sortcxix);
  1379.         AvARRAY(stack)[1] = *SP;
  1380.         stack_sp = stack_base + 1;
  1381.         return 0;
  1382.     }
  1383.     }
  1384.  
  1385.     cxix = dopoptosub(cxstack_ix);
  1386.     if (cxix < 0)
  1387.     DIE("Can't return outside a subroutine");
  1388.     if (cxix < cxstack_ix)
  1389.     dounwind(cxix);
  1390.  
  1391.     POPBLOCK(cx,newpm);
  1392.     switch (cx->cx_type) {
  1393.     case CXt_SUB:
  1394.     POPSUB(cx);
  1395.     break;
  1396.     case CXt_EVAL:
  1397.     POPEVAL(cx);
  1398.     if (optype == OP_REQUIRE &&
  1399.         (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
  1400.     {
  1401.         char *name = cx->blk_eval.old_name;
  1402.         (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
  1403.         DIE("%s did not return a true value", name);
  1404.     }
  1405.     break;
  1406.     default:
  1407.     DIE("panic: return");
  1408.     break;
  1409.     }
  1410.  
  1411.     if (gimme == G_SCALAR) {
  1412.     if (MARK < SP)
  1413.         *++newsp = sv_mortalcopy(*SP);
  1414.     else
  1415.         *++newsp = &sv_undef;
  1416.     }
  1417.     else {
  1418.     while (MARK < SP)
  1419.         *++newsp = sv_mortalcopy(*++MARK);
  1420.     }
  1421.     curpm = newpm;    /* Don't pop $1 et al till now */
  1422.     stack_sp = newsp;
  1423.  
  1424.     LEAVE;
  1425.     return pop_return();
  1426. }
  1427.  
  1428. PP(pp_last)
  1429. {
  1430.     dSP;
  1431.     I32 cxix;
  1432.     register CONTEXT *cx;
  1433.     I32 gimme;
  1434.     I32 optype;
  1435.     OP *nextop;
  1436.     SV **newsp;
  1437.     PMOP *newpm;
  1438.     SV **mark = stack_base + cxstack[cxstack_ix].blk_oldsp;
  1439.  
  1440.     if (op->op_flags & OPf_SPECIAL) {
  1441.     cxix = dopoptoloop(cxstack_ix);
  1442.     if (cxix < 0)
  1443.         DIE("Can't \"last\" outside a block");
  1444.     }
  1445.     else {
  1446.     cxix = dopoptolabel(cPVOP->op_pv);
  1447.     if (cxix < 0)
  1448.         DIE("Label not found for \"last %s\"", cPVOP->op_pv);
  1449.     }
  1450.     if (cxix < cxstack_ix)
  1451.     dounwind(cxix);
  1452.  
  1453.     POPBLOCK(cx,newpm);
  1454.     switch (cx->cx_type) {
  1455.     case CXt_LOOP:
  1456.     POPLOOP(cx);
  1457.     nextop = cx->blk_loop.last_op->op_next;
  1458.     LEAVE;
  1459.     break;
  1460.     case CXt_EVAL:
  1461.     POPEVAL(cx);
  1462.     nextop = pop_return();
  1463.     break;
  1464.     case CXt_SUB:
  1465.     POPSUB(cx);
  1466.     nextop = pop_return();
  1467.     break;
  1468.     default:
  1469.     DIE("panic: last");
  1470.     break;
  1471.     }
  1472.  
  1473.     if (gimme == G_SCALAR) {
  1474.     if (mark < SP)
  1475.         *++newsp = sv_mortalcopy(*SP);
  1476.     else
  1477.         *++newsp = &sv_undef;
  1478.     }
  1479.     else {
  1480.     while (mark < SP)
  1481.         *++newsp = sv_mortalcopy(*++mark);
  1482.     }
  1483.     curpm = newpm;    /* Don't pop $1 et al till now */
  1484.     sp = newsp;
  1485.  
  1486.     LEAVE;
  1487.     RETURNOP(nextop);
  1488. }
  1489.  
  1490. PP(pp_next)
  1491. {
  1492.     I32 cxix;
  1493.     register CONTEXT *cx;
  1494.     I32 oldsave;
  1495.  
  1496.     if (op->op_flags & OPf_SPECIAL) {
  1497.     cxix = dopoptoloop(cxstack_ix);
  1498.     if (cxix < 0)
  1499.         DIE("Can't \"next\" outside a block");
  1500.     }
  1501.     else {
  1502.     cxix = dopoptolabel(cPVOP->op_pv);
  1503.     if (cxix < 0)
  1504.         DIE("Label not found for \"next %s\"", cPVOP->op_pv);
  1505.     }
  1506.     if (cxix < cxstack_ix)
  1507.     dounwind(cxix);
  1508.  
  1509.     TOPBLOCK(cx);
  1510.     oldsave = scopestack[scopestack_ix - 1];
  1511.     LEAVE_SCOPE(oldsave);
  1512.     return cx->blk_loop.next_op;
  1513. }
  1514.  
  1515. PP(pp_redo)
  1516. {
  1517.     I32 cxix;
  1518.     register CONTEXT *cx;
  1519.     I32 oldsave;
  1520.  
  1521.     if (op->op_flags & OPf_SPECIAL) {
  1522.     cxix = dopoptoloop(cxstack_ix);
  1523.     if (cxix < 0)
  1524.         DIE("Can't \"redo\" outside a block");
  1525.     }
  1526.     else {
  1527.     cxix = dopoptolabel(cPVOP->op_pv);
  1528.     if (cxix < 0)
  1529.         DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
  1530.     }
  1531.     if (cxix < cxstack_ix)
  1532.     dounwind(cxix);
  1533.  
  1534.     TOPBLOCK(cx);
  1535.     oldsave = scopestack[scopestack_ix - 1];
  1536.     LEAVE_SCOPE(oldsave);
  1537.     return cx->blk_loop.redo_op;
  1538. }
  1539.  
  1540. static OP* lastgotoprobe;
  1541.  
  1542. static OP *
  1543. dofindlabel(op,label,opstack)
  1544. OP *op;
  1545. char *label;
  1546. OP **opstack;
  1547. {
  1548.     OP *kid;
  1549.     OP **ops = opstack;
  1550.  
  1551.     if (op->op_type == OP_LEAVE ||
  1552.     op->op_type == OP_SCOPE ||
  1553.     op->op_type == OP_LEAVELOOP ||
  1554.     op->op_type == OP_LEAVETRY)
  1555.         *ops++ = cUNOP->op_first;
  1556.     *ops = 0;
  1557.     if (op->op_flags & OPf_KIDS) {
  1558.     /* First try all the kids at this level, since that's likeliest. */
  1559.     for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
  1560.         if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
  1561.             kCOP->cop_label && strEQ(kCOP->cop_label, label))
  1562.         return kid;
  1563.     }
  1564.     for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
  1565.         if (kid == lastgotoprobe)
  1566.         continue;
  1567.         if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
  1568.         if (ops > opstack &&
  1569.           (ops[-1]->op_type == OP_NEXTSTATE ||
  1570.            ops[-1]->op_type == OP_DBSTATE))
  1571.             *ops = kid;
  1572.         else
  1573.             *ops++ = kid;
  1574.         }
  1575.         if (op = dofindlabel(kid,label,ops))
  1576.         return op;
  1577.     }
  1578.     }
  1579.     *ops = 0;
  1580.     return 0;
  1581. }
  1582.  
  1583. PP(pp_dump)
  1584. {
  1585.     return pp_goto(ARGS);
  1586.     /*NOTREACHED*/
  1587. }
  1588.  
  1589. PP(pp_goto)
  1590. {
  1591.     dSP;
  1592.     OP *retop = 0;
  1593.     I32 ix;
  1594.     register CONTEXT *cx;
  1595.     OP *enterops[64];
  1596.     char *label;
  1597.     int do_dump = (op->op_type == OP_DUMP);
  1598.  
  1599.     label = 0;
  1600.     if (op->op_flags & OPf_STACKED) {
  1601.     SV *sv = POPs;
  1602.  
  1603.     /* This egregious kludge implements goto &subroutine */
  1604.     if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
  1605.         I32 cxix;
  1606.         register CONTEXT *cx;
  1607.         CV* cv = (CV*)SvRV(sv);
  1608.         SV** mark;
  1609.         I32 items = 0;
  1610.         I32 oldsave;
  1611.  
  1612.         if (!CvROOT(cv) && !CvXSUB(cv)) {
  1613.         if (CvGV(cv)) {
  1614.             SV *tmpstr = sv_newmortal();
  1615.             gv_efullname(tmpstr, CvGV(cv));
  1616.             DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
  1617.         }
  1618.         DIE("Goto undefined subroutine");
  1619.         }
  1620.  
  1621.         /* First do some returnish stuff. */
  1622.         cxix = dopoptosub(cxstack_ix);
  1623.         if (cxix < 0)
  1624.         DIE("Can't goto subroutine outside a subroutine");
  1625.         if (cxix < cxstack_ix)
  1626.         dounwind(cxix);
  1627.         TOPBLOCK(cx);
  1628.         mark = stack_sp;
  1629.         if (cx->blk_sub.hasargs) {   /* put @_ back onto stack */
  1630.         AV* av = cx->blk_sub.argarray;
  1631.         
  1632.         items = AvFILL(av) + 1;
  1633.         Copy(AvARRAY(av), ++stack_sp, items, SV*);
  1634.         stack_sp += items;
  1635.         GvAV(defgv) = cx->blk_sub.savearray;
  1636.         AvREAL_off(av);
  1637.         av_clear(av);
  1638.         }
  1639.         if (!(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
  1640.         SvREFCNT_dec(cx->blk_sub.cv);
  1641.         oldsave = scopestack[scopestack_ix - 1];
  1642.         LEAVE_SCOPE(oldsave);
  1643.  
  1644.         /* Now do some callish stuff. */
  1645.         SAVETMPS;
  1646.         if (CvXSUB(cv)) {
  1647.         if (CvOLDSTYLE(cv)) {
  1648.             I32 (*fp3)_((int,int,int));
  1649.             while (sp > mark) {
  1650.             sp[1] = sp[0];
  1651.             sp--;
  1652.             }
  1653.             fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
  1654.             items = (*fp3)(CvXSUBANY(cv).any_i32,
  1655.                            mark - stack_base + 1,
  1656.                    items);
  1657.             sp = stack_base + items;
  1658.         }
  1659.         else {
  1660.             (void)(*CvXSUB(cv))(cv);
  1661.         }
  1662.         LEAVE;
  1663.         return pop_return();
  1664.         }
  1665.         else {
  1666.         AV* padlist = CvPADLIST(cv);
  1667.         SV** svp = AvARRAY(padlist);
  1668.         cx->blk_sub.cv = cv;
  1669.         cx->blk_sub.olddepth = CvDEPTH(cv);
  1670.         CvDEPTH(cv)++;
  1671.         if (CvDEPTH(cv) < 2)
  1672.             (void)SvREFCNT_inc(cv);
  1673.         else {    /* save temporaries on recursion? */
  1674.             if (CvDEPTH(cv) == 100 && dowarn)
  1675.             warn("Deep recursion on subroutine \"%s\"",
  1676.                 GvENAME(CvGV(cv)));
  1677.             if (CvDEPTH(cv) > AvFILL(padlist)) {
  1678.             AV *newpad = newAV();
  1679.             SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
  1680.             I32 ix = AvFILL((AV*)svp[1]);
  1681.             svp = AvARRAY(svp[0]);
  1682.             for ( ;ix > 0; ix--) {
  1683.                 if (svp[ix] != &sv_undef) {
  1684.                 char *name = SvPVX(svp[ix]);
  1685.                 if (SvFLAGS(svp[ix]) & SVf_FAKE) {
  1686.                     /* outer lexical? */
  1687.                     av_store(newpad, ix,
  1688.                     SvREFCNT_inc(oldpad[ix]) );
  1689.                 }
  1690.                 else {        /* our own lexical */
  1691.                     if (*name == '@')
  1692.                     av_store(newpad, ix, sv = (SV*)newAV());
  1693.                     else if (*name == '%')
  1694.                     av_store(newpad, ix, sv = (SV*)newHV());
  1695.                     else
  1696.                     av_store(newpad, ix, sv = NEWSV(0,0));
  1697.                     SvPADMY_on(sv);
  1698.                 }
  1699.                 }
  1700.                 else {
  1701.                 av_store(newpad, ix, sv = NEWSV(0,0));
  1702.                 SvPADTMP_on(sv);
  1703.                 }
  1704.             }
  1705.             if (cx->blk_sub.hasargs) {
  1706.                 AV* av = newAV();
  1707.                 av_extend(av, 0);
  1708.                 av_store(newpad, 0, (SV*)av);
  1709.                 AvFLAGS(av) = AVf_REIFY;
  1710.             }
  1711.             av_store(padlist, CvDEPTH(cv), (SV*)newpad);
  1712.             AvFILL(padlist) = CvDEPTH(cv);
  1713.             svp = AvARRAY(padlist);
  1714.             }
  1715.         }
  1716.         SAVESPTR(curpad);
  1717.         curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
  1718.         if (cx->blk_sub.hasargs) {
  1719.             AV* av = (AV*)curpad[0];
  1720.             SV** ary;
  1721.  
  1722.             cx->blk_sub.savearray = GvAV(defgv);
  1723.             cx->blk_sub.argarray = av;
  1724.             GvAV(defgv) = cx->blk_sub.argarray;
  1725.             ++mark;
  1726.  
  1727.             if (items >= AvMAX(av) + 1) {
  1728.             ary = AvALLOC(av);
  1729.             if (AvARRAY(av) != ary) {
  1730.                 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
  1731.                 SvPVX(av) = (char*)ary;
  1732.             }
  1733.             if (items >= AvMAX(av) + 1) {
  1734.                 AvMAX(av) = items - 1;
  1735.                 Renew(ary,items+1,SV*);
  1736.                 AvALLOC(av) = ary;
  1737.                 SvPVX(av) = (char*)ary;
  1738.             }
  1739.             }
  1740.             Copy(mark,AvARRAY(av),items,SV*);
  1741.             AvFILL(av) = items - 1;
  1742.             
  1743.             while (items--) {
  1744.             if (*mark)
  1745.                 SvTEMP_off(*mark);
  1746.             mark++;
  1747.             }
  1748.         }
  1749.         RETURNOP(CvSTART(cv));
  1750.         }
  1751.     }
  1752.     else
  1753.         label = SvPV(sv,na);
  1754.     }
  1755.     else if (op->op_flags & OPf_SPECIAL) {
  1756.     if (! do_dump)
  1757.         DIE("goto must have label");
  1758.     }
  1759.     else
  1760.     label = cPVOP->op_pv;
  1761.  
  1762.     if (label && *label) {
  1763.     OP *gotoprobe = 0;
  1764.  
  1765.     /* find label */
  1766.  
  1767.     lastgotoprobe = 0;
  1768.     *enterops = 0;
  1769.     for (ix = cxstack_ix; ix >= 0; ix--) {
  1770.         cx = &cxstack[ix];
  1771.         switch (cx->cx_type) {
  1772.         case CXt_SUB:
  1773.         gotoprobe = CvROOT(cx->blk_sub.cv);
  1774.         break;
  1775.         case CXt_EVAL:
  1776.         gotoprobe = eval_root; /* XXX not good for nested eval */
  1777.         break;
  1778.         case CXt_LOOP:
  1779.         gotoprobe = cx->blk_oldcop->op_sibling;
  1780.         break;
  1781.         case CXt_SUBST:
  1782.         continue;
  1783.         case CXt_BLOCK:
  1784.         if (ix)
  1785.             gotoprobe = cx->blk_oldcop->op_sibling;
  1786.         else
  1787.             gotoprobe = main_root;
  1788.         break;
  1789.         default:
  1790.         if (ix)
  1791.             DIE("panic: goto");
  1792.         else
  1793.             gotoprobe = main_root;
  1794.         break;
  1795.         }
  1796.         retop = dofindlabel(gotoprobe, label, enterops);
  1797.         if (retop)
  1798.         break;
  1799.         lastgotoprobe = gotoprobe;
  1800.     }
  1801.     if (!retop)
  1802.         DIE("Can't find label %s", label);
  1803.  
  1804.     /* pop unwanted frames */
  1805.  
  1806.     if (ix < cxstack_ix) {
  1807.         I32 oldsave;
  1808.  
  1809.         if (ix < 0)
  1810.         ix = 0;
  1811.         dounwind(ix);
  1812.         TOPBLOCK(cx);
  1813.         oldsave = scopestack[scopestack_ix];
  1814.         LEAVE_SCOPE(oldsave);
  1815.     }
  1816.  
  1817.     /* push wanted frames */
  1818.  
  1819.     if (*enterops && enterops[1]) {
  1820.         OP *oldop = op;
  1821.         for (ix = 1; enterops[ix]; ix++) {
  1822.         op = enterops[ix];
  1823.         (*op->op_ppaddr)();
  1824.         }
  1825.         op = oldop;
  1826.     }
  1827.     }
  1828.  
  1829.     if (do_dump) {
  1830. #ifdef VMS
  1831.     if (!retop) retop = main_start;
  1832. #endif
  1833.     restartop = retop;
  1834.     do_undump = TRUE;
  1835.  
  1836.     my_unexec();
  1837.  
  1838.     restartop = 0;        /* hmm, must be GNU unexec().. */
  1839.     do_undump = FALSE;
  1840.     }
  1841.  
  1842.     if (stack == signalstack) {
  1843.         restartop = retop;
  1844.         Siglongjmp(top_env, 3);
  1845.     }
  1846.  
  1847.     RETURNOP(retop);
  1848. }
  1849.  
  1850. PP(pp_exit)
  1851. {
  1852.     dSP;
  1853.     I32 anum;
  1854.  
  1855.     if (MAXARG < 1)
  1856.     anum = 0;
  1857.     else
  1858.     anum = SvIVx(POPs);
  1859.     my_exit(anum);
  1860.     PUSHs(&sv_undef);
  1861.     RETURN;
  1862. }
  1863.  
  1864. #ifdef NOTYET
  1865. PP(pp_nswitch)
  1866. {
  1867.     dSP;
  1868.     double value = SvNVx(GvSV(cCOP->cop_gv));
  1869.     register I32 match = I_32(value);
  1870.  
  1871.     if (value < 0.0) {
  1872.     if (((double)match) > value)
  1873.         --match;        /* was fractional--truncate other way */
  1874.     }
  1875.     match -= cCOP->uop.scop.scop_offset;
  1876.     if (match < 0)
  1877.     match = 0;
  1878.     else if (match > cCOP->uop.scop.scop_max)
  1879.     match = cCOP->uop.scop.scop_max;
  1880.     op = cCOP->uop.scop.scop_next[match];
  1881.     RETURNOP(op);
  1882. }
  1883.  
  1884. PP(pp_cswitch)
  1885. {
  1886.     dSP;
  1887.     register I32 match;
  1888.  
  1889.     if (multiline)
  1890.     op = op->op_next;            /* can't assume anything */
  1891.     else {
  1892.     match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
  1893.     match -= cCOP->uop.scop.scop_offset;
  1894.     if (match < 0)
  1895.         match = 0;
  1896.     else if (match > cCOP->uop.scop.scop_max)
  1897.         match = cCOP->uop.scop.scop_max;
  1898.     op = cCOP->uop.scop.scop_next[match];
  1899.     }
  1900.     RETURNOP(op);
  1901. }
  1902. #endif
  1903.  
  1904. /* Eval. */
  1905.  
  1906. static void
  1907. save_lines(array, sv)
  1908. AV *array;
  1909. SV *sv;
  1910. {
  1911.     register char *s = SvPVX(sv);
  1912.     register char *send = SvPVX(sv) + SvCUR(sv);
  1913.     register char *t;
  1914.     register I32 line = 1;
  1915.  
  1916.     while (s && s < send) {
  1917.     SV *tmpstr = NEWSV(85,0);
  1918.  
  1919.     sv_upgrade(tmpstr, SVt_PVMG);
  1920.     t = strchr(s, '\n');
  1921.     if (t)
  1922.         t++;
  1923.     else
  1924.         t = send;
  1925.  
  1926.     sv_setpvn(tmpstr, s, t - s);
  1927.     av_store(array, line++, tmpstr);
  1928.     s = t;
  1929.     }
  1930. }
  1931.  
  1932. static OP *
  1933. doeval(gimme)
  1934. int gimme;
  1935. {
  1936.     dSP;
  1937.     OP *saveop = op;
  1938.     HV *newstash;
  1939.     AV* comppadlist;
  1940.  
  1941.     in_eval = 1;
  1942.  
  1943.     /* set up a scratch pad */
  1944.  
  1945.     SAVEINT(padix);
  1946.     SAVESPTR(curpad);
  1947.     SAVESPTR(comppad);
  1948.     SAVESPTR(comppad_name);
  1949.     SAVEINT(comppad_name_fill);
  1950.     SAVEINT(min_intro_pending);
  1951.     SAVEINT(max_intro_pending);
  1952.  
  1953.     SAVESPTR(compcv);
  1954.     compcv = (CV*)NEWSV(1104,0);
  1955.     sv_upgrade((SV *)compcv, SVt_PVCV);
  1956.  
  1957.     comppad = newAV();
  1958.     comppad_name = newAV();
  1959.     comppad_name_fill = 0;
  1960.     min_intro_pending = 0;
  1961.     av_push(comppad, Nullsv);
  1962.     curpad = AvARRAY(comppad);
  1963.     padix = 0;
  1964.  
  1965.     comppadlist = newAV();
  1966.     AvREAL_off(comppadlist);
  1967.     av_store(comppadlist, 0, (SV*)comppad_name);
  1968.     av_store(comppadlist, 1, (SV*)comppad);
  1969.     CvPADLIST(compcv) = comppadlist;
  1970.     SAVEFREESV(compcv);
  1971.  
  1972.     /* make sure we compile in the right package */
  1973.  
  1974.     newstash = curcop->cop_stash;
  1975.     if (curstash != newstash) {
  1976.     SAVESPTR(curstash);
  1977.     curstash = newstash;
  1978.     }
  1979.     SAVESPTR(beginav);
  1980.     beginav = newAV();
  1981.     SAVEFREESV(beginav);
  1982.  
  1983.     /* try to compile it */
  1984.  
  1985.     eval_root = Nullop;
  1986.     error_count = 0;
  1987.     curcop = &compiling;
  1988.     curcop->cop_arybase = 0;
  1989.     SvREFCNT_dec(rs);
  1990.     rs = newSVpv("\n", 1);
  1991.     sv_setpv(GvSV(errgv),"");
  1992.     if (yyparse() || error_count || !eval_root) {
  1993.     SV **newsp;
  1994.     I32 gimme;
  1995.     CONTEXT *cx;
  1996.     I32 optype;
  1997.  
  1998.     op = saveop;
  1999.     if (eval_root) {
  2000.         op_free(eval_root);
  2001.         eval_root = Nullop;
  2002.     }
  2003.     POPBLOCK(cx,curpm);
  2004.     POPEVAL(cx);
  2005.     pop_return();
  2006.     lex_end();
  2007.     LEAVE;
  2008.     if (optype == OP_REQUIRE)
  2009.         DIE("%s", SvPVx(GvSV(errgv), na));
  2010.     SvREFCNT_dec(rs);
  2011.     rs = SvREFCNT_inc(nrs);
  2012.     RETPUSHUNDEF;
  2013.     }
  2014.     SvREFCNT_dec(rs);
  2015.     rs = SvREFCNT_inc(nrs);
  2016.     compiling.cop_line = 0;
  2017.     SAVEFREEOP(eval_root);
  2018.     if (gimme & G_ARRAY)
  2019.     list(eval_root);
  2020.     else
  2021.     scalar(eval_root);
  2022.  
  2023.     DEBUG_x(dump_eval());
  2024.  
  2025.     /* compiled okay, so do it */
  2026.  
  2027.     RETURNOP(eval_start);
  2028. }
  2029.  
  2030. PP(pp_require)
  2031. {
  2032.     dSP;
  2033.     register CONTEXT *cx;
  2034.     SV *sv;
  2035.     char *name;
  2036.     char *tmpname;
  2037.     SV** svp;
  2038.     I32 gimme = G_SCALAR;
  2039.     FILE *tryrsfp = 0;
  2040.  
  2041.     sv = POPs;
  2042.     if (SvNIOKp(sv) && !SvPOKp(sv)) {
  2043.     if (atof(patchlevel) + 0.00000999 < SvNV(sv))
  2044.         DIE("Perl %s required--this is only version %s, stopped",
  2045.         SvPV(sv,na),patchlevel);
  2046.     RETPUSHYES;
  2047.     }
  2048.     name = SvPV(sv, na);
  2049.     if (!*name)
  2050.     DIE("Null filename used");
  2051.     TAINT_PROPER("require");
  2052.     if (op->op_type == OP_REQUIRE &&
  2053.       (svp = hv_fetch(GvHVn(incgv), name, SvCUR(sv), 0)) &&
  2054.       *svp != &sv_undef)
  2055.     RETPUSHYES;
  2056.  
  2057.     /* prepare to compile file */
  2058.  
  2059.     tmpname = savepv(name);
  2060.     if (*tmpname == '/' ||
  2061.     (*tmpname == '.' && 
  2062.         (tmpname[1] == '/' ||
  2063.          (tmpname[1] == '.' && tmpname[2] == '/')))
  2064. #ifdef DOSISH
  2065.       || (tmpname[0] && tmpname[1] == ':')
  2066. #endif
  2067. #ifdef VMS
  2068.     || (strchr(tmpname,':') || ((*tmpname == '[' || *tmpname == '<') &&
  2069.         (tmpname[1] == '-' || tmpname[1] == ']' || tmpname[1] == '>')))
  2070. #endif
  2071.     )
  2072.     {
  2073.     tryrsfp = fopen(tmpname,"r");
  2074.     }
  2075.     else {
  2076.     AV *ar = GvAVn(incgv);
  2077.     I32 i;
  2078.  
  2079.     for (i = 0; i <= AvFILL(ar); i++) {
  2080. #ifdef VMS
  2081.         if (tounixpath_ts(SvPVx(*av_fetch(ar, i, TRUE), na),buf) == NULL)
  2082.         continue;
  2083.         strcat(buf,name);
  2084. #else
  2085.         (void)sprintf(buf, "%s/%s",
  2086.         SvPVx(*av_fetch(ar, i, TRUE), na), name);
  2087. #endif
  2088.         tryrsfp = fopen(buf, "r");
  2089.         if (tryrsfp) {
  2090.         char *s = buf;
  2091.  
  2092.         if (*s == '.' && s[1] == '/')
  2093.             s += 2;
  2094.         Safefree(tmpname);
  2095.         tmpname = savepv(s);
  2096.         break;
  2097.         }
  2098.     }
  2099.     }
  2100.     SAVESPTR(compiling.cop_filegv);
  2101.     compiling.cop_filegv = gv_fetchfile(tmpname);
  2102.     Safefree(tmpname);
  2103.     tmpname = Nullch;
  2104.     if (!tryrsfp) {
  2105.     if (op->op_type == OP_REQUIRE) {
  2106.         sprintf(tokenbuf,"Can't locate %s in @INC", name);
  2107.         if (instr(tokenbuf,".h "))
  2108.         strcat(tokenbuf," (change .h to .ph maybe?)");
  2109.         if (instr(tokenbuf,".ph "))
  2110.         strcat(tokenbuf," (did you run h2ph?)");
  2111.         DIE("%s",tokenbuf);
  2112.     }
  2113.  
  2114.     RETPUSHUNDEF;
  2115.     }
  2116.  
  2117.     /* Assume success here to prevent recursive requirement. */
  2118.     (void)hv_store(GvHVn(incgv), name, strlen(name),
  2119.     newSVsv(GvSV(compiling.cop_filegv)), 0 );
  2120.  
  2121.     ENTER;
  2122.     SAVETMPS;
  2123.     lex_start(sv_2mortal(newSVpv("",0)));
  2124.     if (rsfp_filters){
  2125.      save_aptr(&rsfp_filters);
  2126.     rsfp_filters = NULL;
  2127.     }
  2128.  
  2129.     rsfp = tryrsfp;
  2130.     name = savepv(name);
  2131.     SAVEFREEPV(name);
  2132.     SAVEI32(hints);
  2133.     hints = 0;
  2134.  
  2135.     /* switch to eval mode */
  2136.  
  2137.     push_return(op->op_next);
  2138.     PUSHBLOCK(cx, CXt_EVAL, SP);
  2139.     PUSHEVAL(cx, name, compiling.cop_filegv);
  2140.  
  2141.     compiling.cop_line = 0;
  2142.  
  2143.     PUTBACK;
  2144.     return doeval(G_SCALAR);
  2145. }
  2146.  
  2147. PP(pp_dofile)
  2148. {
  2149.     return pp_require(ARGS);
  2150. }
  2151.  
  2152. PP(pp_entereval)
  2153. {
  2154.     dSP;
  2155.     register CONTEXT *cx;
  2156.     dPOPss;
  2157.     I32 gimme = GIMME;
  2158.     char tmpbuf[32];
  2159.     STRLEN len;
  2160.  
  2161.     if (!SvPV(sv,len) || !len)
  2162.     RETPUSHUNDEF;
  2163.     TAINT_PROPER("eval");
  2164.  
  2165.     ENTER;
  2166.     lex_start(sv);
  2167.     SAVETMPS;
  2168.  
  2169.     /* switch to eval mode */
  2170.  
  2171.     SAVESPTR(compiling.cop_filegv);
  2172.     sprintf(tmpbuf, "_<(eval %d)", ++evalseq);
  2173.     compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
  2174.     compiling.cop_line = 1;
  2175.     SAVEDELETE(defstash, savepv(tmpbuf), strlen(tmpbuf));
  2176.     SAVEI32(hints);
  2177.     hints = op->op_targ;
  2178.  
  2179.     push_return(op->op_next);
  2180.     PUSHBLOCK(cx, CXt_EVAL, SP);
  2181.     PUSHEVAL(cx, 0, compiling.cop_filegv);
  2182.  
  2183.     /* prepare to compile string */
  2184.  
  2185.     if (perldb && curstash != debstash)
  2186.     save_lines(GvAV(compiling.cop_filegv), linestr);
  2187.     PUTBACK;
  2188.     return doeval(gimme);
  2189. }
  2190.  
  2191. PP(pp_leaveeval)
  2192. {
  2193.     dSP;
  2194.     register SV **mark;
  2195.     SV **newsp;
  2196.     PMOP *newpm;
  2197.     I32 gimme;
  2198.     register CONTEXT *cx;
  2199.     OP *retop;
  2200.     I32 optype;
  2201.  
  2202.     POPBLOCK(cx,newpm);
  2203.     POPEVAL(cx);
  2204.     retop = pop_return();
  2205.  
  2206.     if (gimme == G_SCALAR) {
  2207.     if (op->op_private & OPpLEAVE_VOID)
  2208.         MARK = newsp;
  2209.     else {
  2210.         MARK = newsp + 1;
  2211.         if (MARK <= SP) {
  2212.         if (SvFLAGS(TOPs) & SVs_TEMP)
  2213.             *MARK = TOPs;
  2214.         else
  2215.             *MARK = sv_mortalcopy(TOPs);
  2216.         }
  2217.         else {
  2218.         MEXTEND(mark,0);
  2219.         *MARK = &sv_undef;
  2220.         }
  2221.     }
  2222.     SP = MARK;
  2223.     }
  2224.     else {
  2225.     for (mark = newsp + 1; mark <= SP; mark++)
  2226.         if (!(SvFLAGS(TOPs) & SVs_TEMP))
  2227.         *mark = sv_mortalcopy(*mark);
  2228.         /* in case LEAVE wipes old return values */
  2229.     }
  2230.     curpm = newpm;    /* Don't pop $1 et al till now */
  2231.  
  2232.     if (optype != OP_ENTEREVAL) {
  2233.     char *name = cx->blk_eval.old_name;
  2234.  
  2235.     if (!(gimme == G_SCALAR ? SvTRUE(*sp) : sp > newsp)) {
  2236.         /* Unassume the success we assumed earlier. */
  2237.         (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
  2238.  
  2239.         if (optype == OP_REQUIRE)
  2240.         retop = die("%s did not return a true value", name);
  2241.     }
  2242.     }
  2243.  
  2244.     lex_end();
  2245.     LEAVE;
  2246.     sv_setpv(GvSV(errgv),"");
  2247.  
  2248.     RETURNOP(retop);
  2249. }
  2250.  
  2251. PP(pp_entertry)
  2252. {
  2253.     dSP;
  2254.     register CONTEXT *cx;
  2255.     I32 gimme = GIMME;
  2256.  
  2257.     ENTER;
  2258.     SAVETMPS;
  2259.  
  2260.     push_return(cLOGOP->op_other->op_next);
  2261.     PUSHBLOCK(cx, CXt_EVAL, SP);
  2262.     PUSHEVAL(cx, 0, 0);
  2263.     eval_root = op;        /* Only needed so that goto works right. */
  2264.  
  2265.     in_eval = 1;
  2266.     sv_setpv(GvSV(errgv),"");
  2267.     RETURN;
  2268. }
  2269.  
  2270. PP(pp_leavetry)
  2271. {
  2272.     dSP;
  2273.     register SV **mark;
  2274.     SV **newsp;
  2275.     PMOP *newpm;
  2276.     I32 gimme;
  2277.     register CONTEXT *cx;
  2278.     I32 optype;
  2279.  
  2280.     POPBLOCK(cx,newpm);
  2281.     POPEVAL(cx);
  2282.     pop_return();
  2283.  
  2284.     if (gimme == G_SCALAR) {
  2285.     if (op->op_private & OPpLEAVE_VOID)
  2286.         MARK = newsp;
  2287.     else {
  2288.         MARK = newsp + 1;
  2289.         if (MARK <= SP) {
  2290.         if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
  2291.             *MARK = TOPs;
  2292.         else
  2293.             *MARK = sv_mortalcopy(TOPs);
  2294.         }
  2295.         else {
  2296.         MEXTEND(mark,0);
  2297.         *MARK = &sv_undef;
  2298.         }
  2299.     }
  2300.     SP = MARK;
  2301.     }
  2302.     else {
  2303.     for (mark = newsp + 1; mark <= SP; mark++)
  2304.         if (!(SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP)))
  2305.         *mark = sv_mortalcopy(*mark);
  2306.         /* in case LEAVE wipes old return values */
  2307.     }
  2308.     curpm = newpm;    /* Don't pop $1 et al till now */
  2309.  
  2310.     LEAVE;
  2311.     sv_setpv(GvSV(errgv),"");
  2312.     RETURN;
  2313. }
  2314.  
  2315. static void
  2316. doparseform(sv)
  2317. SV *sv;
  2318. {
  2319.     STRLEN len;
  2320.     register char *s = SvPV_force(sv, len);
  2321.     register char *send = s + len;
  2322.     register char *base;
  2323.     register I32 skipspaces = 0;
  2324.     bool noblank;
  2325.     bool repeat;
  2326.     bool postspace = FALSE;
  2327.     U16 *fops;
  2328.     register U16 *fpc;
  2329.     U16 *linepc;
  2330.     register I32 arg;
  2331.     bool ischop;
  2332.  
  2333.     New(804, fops, (send - s)*3+2, U16);    /* Almost certainly too long... */
  2334.     fpc = fops;
  2335.  
  2336.     if (s < send) {
  2337.     linepc = fpc;
  2338.     *fpc++ = FF_LINEMARK;
  2339.     noblank = repeat = FALSE;
  2340.     base = s;
  2341.     }
  2342.  
  2343.     while (s <= send) {
  2344.     switch (*s++) {
  2345.     default:
  2346.         skipspaces = 0;
  2347.         continue;
  2348.  
  2349.     case '~':
  2350.         if (*s == '~') {
  2351.         repeat = TRUE;
  2352.         *s = ' ';
  2353.         }
  2354.         noblank = TRUE;
  2355.         s[-1] = ' ';
  2356.         /* FALL THROUGH */
  2357.     case ' ': case '\t':
  2358.         skipspaces++;
  2359.         continue;
  2360.         
  2361.     case '\n': case 0:
  2362.         arg = s - base;
  2363.         skipspaces++;
  2364.         arg -= skipspaces;
  2365.         if (arg) {
  2366.         if (postspace) {
  2367.             *fpc++ = FF_SPACE;
  2368.             postspace = FALSE;
  2369.         }
  2370.         *fpc++ = FF_LITERAL;
  2371.         *fpc++ = arg;
  2372.         }
  2373.         if (s <= send)
  2374.         skipspaces--;
  2375.         if (skipspaces) {
  2376.         *fpc++ = FF_SKIP;
  2377.         *fpc++ = skipspaces;
  2378.         }
  2379.         skipspaces = 0;
  2380.         if (s <= send)
  2381.         *fpc++ = FF_NEWLINE;
  2382.         if (noblank) {
  2383.         *fpc++ = FF_BLANK;
  2384.         if (repeat)
  2385.             arg = fpc - linepc + 1;
  2386.         else
  2387.             arg = 0;
  2388.         *fpc++ = arg;
  2389.         }
  2390.         if (s < send) {
  2391.         linepc = fpc;
  2392.         *fpc++ = FF_LINEMARK;
  2393.         noblank = repeat = FALSE;
  2394.         base = s;
  2395.         }
  2396.         else
  2397.         s++;
  2398.         continue;
  2399.  
  2400.     case '@':
  2401.     case '^':
  2402.         ischop = s[-1] == '^';
  2403.  
  2404.         if (postspace) {
  2405.         *fpc++ = FF_SPACE;
  2406.         postspace = FALSE;
  2407.         }
  2408.         arg = (s - base) - 1;
  2409.         if (arg) {
  2410.         *fpc++ = FF_LITERAL;
  2411.         *fpc++ = arg;
  2412.         }
  2413.  
  2414.         base = s - 1;
  2415.         *fpc++ = FF_FETCH;
  2416.         if (*s == '*') {
  2417.         s++;
  2418.         *fpc++ = 0;
  2419.         *fpc++ = FF_LINEGLOB;
  2420.         }
  2421.         else if (*s == '#' || (*s == '.' && s[1] == '#')) {
  2422.         arg = ischop ? 512 : 0;
  2423.         base = s - 1;
  2424.         while (*s == '#')
  2425.             s++;
  2426.         if (*s == '.') {
  2427.             char *f;
  2428.             s++;
  2429.             f = s;
  2430.             while (*s == '#')
  2431.             s++;
  2432.             arg |= 256 + (s - f);
  2433.         }
  2434.         *fpc++ = s - base;        /* fieldsize for FETCH */
  2435.         *fpc++ = FF_DECIMAL;
  2436.         *fpc++ = arg;
  2437.         }
  2438.         else {
  2439.         I32 prespace = 0;
  2440.         bool ismore = FALSE;
  2441.  
  2442.         if (*s == '>') {
  2443.             while (*++s == '>') ;
  2444.             prespace = FF_SPACE;
  2445.         }
  2446.         else if (*s == '|') {
  2447.             while (*++s == '|') ;
  2448.             prespace = FF_HALFSPACE;
  2449.             postspace = TRUE;
  2450.         }
  2451.         else {
  2452.             if (*s == '<')
  2453.             while (*++s == '<') ;
  2454.             postspace = TRUE;
  2455.         }
  2456.         if (*s == '.' && s[1] == '.' && s[2] == '.') {
  2457.             s += 3;
  2458.             ismore = TRUE;
  2459.         }
  2460.         *fpc++ = s - base;        /* fieldsize for FETCH */
  2461.  
  2462.         *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
  2463.  
  2464.         if (prespace)
  2465.             *fpc++ = prespace;
  2466.         *fpc++ = FF_ITEM;
  2467.         if (ismore)
  2468.             *fpc++ = FF_MORE;
  2469.         if (ischop)
  2470.             *fpc++ = FF_CHOP;
  2471.         }
  2472.         base = s;
  2473.         skipspaces = 0;
  2474.         continue;
  2475.     }
  2476.     }
  2477.     *fpc++ = FF_END;
  2478.  
  2479.     arg = fpc - fops;
  2480.     { /* need to jump to the next word */
  2481.         int z;
  2482.     z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
  2483.     SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
  2484.     s = SvPVX(sv) + SvCUR(sv) + z;
  2485.     }
  2486.     Copy(fops, s, arg, U16);
  2487.     Safefree(fops);
  2488.     SvCOMPILED_on(sv);
  2489. }
  2490.